FROM alpine:3.7

ENV NGINX_VERSION 1.15.3
ENV NGINX_RTMP_VERSION 1.2.1
# ENV FFMPEG_VERSION 4.0.2
ARG ALPINE_URL=mirrors.aliyun.com

EXPOSE 1935
EXPOSE 80

RUN sed -i "s/dl-cdn.alpinelinux.org/${ALPINE_URL}/g" /etc/apk/repositories

# Build dependencies.
RUN	apk update && apk add	\
  binutils \
  binutils-libs \
  build-base \
  ca-certificates \
  gcc \
  libc-dev \
  libgcc \
  make \
  musl-dev \
  openssl \
  openssl-dev \
  pcre \
  pcre-dev \
  pkgconf \
  pkgconfig \
  zlib-dev \
  tzdata \
  python3-dev

# nginx
WORKDIR /usr/local/nginx

RUN mkdir -p /var/data && mkdir -p /www/static && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

ADD nginx/nginx-${NGINX_VERSION}.tar.gz .
ADD nginx/v1.2.1.tar.gz .

# RUN wget http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz && \
# tar zxf nginx-${NGINX_VERSION}.tar.gz && rm nginx-${NGINX_VERSION}.tar.gz
# RUN wget https://github.com/arut/nginx-rtmp-module/archive/v${NGINX_RTMP_VERSION}.tar.gz && \ 
# tar zxf v${NGINX_RTMP_VERSION}.tar.gz && rm v${NGINX_RTMP_VERSION}.tar.gz

# Compile nginx with nginx-rtmp module.
RUN cd nginx-${NGINX_VERSION} && \
  ./configure \
  --prefix=/usr/local/nginx \
  --with-select_module \
  --with-poll_module \
#  --with-file-aio \
  --with-http_ssl_module \
  --with-http_realip_module \
  --with-http_gzip_static_module \
  --with-http_secure_link_module \
  --with-http_sub_module \
  --with-http_stub_status_module \
#  --with-http_perl_module \
  --with-http_mp4_module \
  --with-http_flv_module \
  --add-module=/usr/local/nginx/nginx-rtmp-module-${NGINX_RTMP_VERSION} \
  --conf-path=/usr/local/nginx/nginx.conf \
  --error-log-path=/usr/local/nginx/logs/error.log \
  --http-log-path=/usr/local/nginx/logs/access.log \
  --with-debug && \
  make && make install

# Cleanup.
RUN rm -rf /var/cache/* /usr/local/nginx/nginx-${NGINX_VERSION}

# python
RUN echo 1
COPY python /python

WORKDIR /python/www

RUN cd /python/www && \
  mkdir /data && \
  pip3 install --upgrade pip -i https://pypi.douban.com/simple && \
  pip3 install -r ../requirements.txt -i https://pypi.douban.com/simple && \
  # python3 db.py && \
  mkdir logs

COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
CMD ["/entrypoint.sh"]
